const path=require('path')

const upload = require("../uploadConfig");
const co = require("co");
const OSS = require("ali-oss");
const fs = require("fs");

let client = new OSS({
    region: "oss-cn-hangzhou",
    accessKeyId: "LTAI5tKYVomNFNockrq3fx1U",
    accessKeySecret: "OfpURZOpTTZS9yYT5adX1uDqU8gOIP",
});
let ali_oss = {
    bucket: "bucket-demo1215",
    endPoint: "oss-cn-zhangjiakou.aliyuncs.com/storytitle",
    cname:true
};

class HomeCtl{
    index(ctx){
        ctx.body='这是主页1'
    }
    async upload(ctx){
        // const file=ctx.request.files.file
        // const basename=path.basename(file.path)
        // ctx.body={
        //     path:{
        //         url:`${ctx.origin}/uploads/${basename}`
        //     }
        // }

        const file=ctx.request.files.file
        const basename=path.basename(file.path)
        var key=basename
        var localFile="./app/public/uploads/"+key
        await co(function* () {
            client.useBucket(ali_oss.bucket);
           // client.put上传图片
            var result = yield client.put(key, localFile);
            // 删除本地储存的图片
            fs.unlinkSync(localFile);
        });
        ctx.body = {
            code: 200,
            msg: "ok",
            filename: "https://bucket-demo1215.oss-cn-hangzhou.aliyuncs.com/" + key,
        };


    }
}

module.exports=new HomeCtl()